<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false"%>
<%@ include file="/common/backcommon.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>Breaking Fast</title>
		<script type="text/javascript">
			function initSelectDish() {
				var selectDishInfo = window.dialogArguments;
				if(null != selectDishInfo && 0 != selectDishInfo.length){
					var dishInfoArr = selectDishInfo.split("_");
					$("#dishIds").val(dishInfoArr[0]);
					$("#dishNames").val(dishInfoArr[1]);
				}
			}
	        function loadPage(totalCount, pageSize, pageCurrent) {
		        $("#page").smartpaginator({
                    totalrecords: totalCount, 
                    recordsperpage: pageSize,
                    initval: pageCurrent, 
                    next: '下一页', 
                    prev: '上一页', 
                    first: '第一页', 
                    last: '最后一页', 
                    theme: 'black', 
                    go: 'Go',
                    onchange: onPageChange
				});
	        };
			
			function onPageChange(newPageValue) {
				//获取查询参数
				var dishChName = $("#dishChName").val();
				var dishEnName = $("#dishEnName").val();
				var priceMax = $("#priceMax").val();
				var priceMin = $("#priceMin").val();
				var startDate = $("#startDate").val();
				var endDate = $("#endDate").val();
				var params = {
					dishChName : dishChName,
					dishEnName : dishEnName,
					priceMax : priceMax,
					priceMin : priceMin,
					startDate : startDate,
					endDate : endDate,
					pageNumber : newPageValue
				};
				var dataTr = $("#pageList tr");
				for(var i = 0; i < dataTr.length; i++) {
					if(i != 0) {
						$(dataTr[i]).remove();
					}
				}
				$.ajax({
					url: "${base}/backDishCtl/select",
					type: "post",
					data: params,
					success: function(result){
						var dishIds = "," + $("#dishIds").val() + ",";
						var dataTable = $("#pageList");
						for(var i = 0; i < result.resultList.length; i++) {
							var trClassName = ""; 
							if(i%2 == 0){
								trClassName = "list_tab_left";
							}
							var dishid = result.resultList[i].dishid;
							var dishchname = result.resultList[i].dishchname;
							var dishenname = result.resultList[i].dishenname;
							var price = result.resultList[i].price;
							var typename = result.resultList[i].dishtype.typechname;
							var createdate = result.resultList[i].createdateStr;
							var username = result.resultList[i].createuser.username;
							//该菜品是否选中
							var dishIdCheckIndex = dishIds.indexOf("," + dishid + ",");
							var checked = dishIdCheckIndex != -1 ? "checked" : "";
							var row = $("<tr class='" + trClassName + "'><td><input type='checkbox' onchange='selectDish(this)' " + checked + " value='" + dishid + "_" + dishchname + "' /></td><td>" + dishchname + "</td><td>" + dishenname + "</td><td>" + price + "</td>" + 
								"<td>" + typename + "</td></tr>");
							dataTable.append(row);
						}
						$("#pageList tr:even").addClass("bg_color");
						loadPage(result.totalCount, result.pageSize, result.pageCurrent);
					}
				});
			};
			
			function selectDish(dish){
				var dishInfoArray = dish.value.split("_");
				var dishId = dishInfoArray[0];
				var dishName = dishInfoArray[1];
				var dishIds = $("#dishIds").val();
				var dishNames =  $("#dishNames").val();
				if(true == dish.checked){
					if(-1 == ("," + dishIds + ",").indexOf("," + dishId + ",")){
						if(0 == dishIds.length){
							dishIds = dishId;
							dishNames = dishName;
						} else {
							dishIds = dishIds + "," + dishId;
							dishNames = dishNames + "," + dishName;
						}
					}
				} else {
					if(0 != dishIds.length){
						dishIds = "," + dishIds + ",";
						dishNames = "," + dishNames + ",";
						dishIds = dishIds.replace("," + dishId + ",", ",");
						dishNames = dishNames.replace("," + dishName + ",", ",");
						//去除收尾逗号
						dishIds = dishIds.substr(1);
						dishIds = dishIds.substr(0, dishIds.length - 1);
						dishNames = dishNames.substr(1);
						dishNames = dishNames.substr(0, dishNames.length - 1);
					}
				}
				$("#dishIds").val(dishIds);
				$("#dishNames").val(dishNames);
			}
			
			$(function() {
				initSelectDish();
				
				onPageChange(1);
				
				$("#queryBtn").click(function() {
					if($.formValidator.pageIsValid("1")) {
						var pageNumber = $("#pageNumber").val();
						onPageChange(pageNumber);
					}
				});
				
				$("#saveBtn").click(function() {
					var dishIds = $("#dishIds").val();
					var dishNames =  $("#dishNames").val();
					var dishValue = "";
					if(0 != dishIds.length) {
						dishValue = dishIds + "_" + dishNames;
					}
					window.returnValue = dishValue;
					window.close();
				});
				
				$("#chkAll").change(function(){
					var chkList = $("#pageList").find("input[type='checkbox']").not($("#chkAll"));
					var dishIds = $("#dishIds").val();
					var dishNames =  $("#dishNames").val();
					for(var i = 0; i < chkList.length; i++) {
						chkList[i].checked = this.checked;
						var dishInfoArray = chkList[i].value.split("_");
						var dishId = dishInfoArray[0];
						var dishName = dishInfoArray[1];
						if(true == this.checked) {
							if(-1 == ("," + dishIds + ",").indexOf("," + dishId + ",")){
								if(0 != dishIds.length) {
									dishIds += ",";
									dishNames += ",";
								}
								dishIds += dishId;
								dishNames += dishName;
							}
						} else {
							if(0 == i && 0 != dishNames.length) {
								dishIds = "," + dishIds + ",";
								dishNames = "," + dishNames + ",";
							}
							dishIds = dishIds.replace("," + dishId + ",", ",");
							dishNames = dishNames.replace("," + dishName + ",", ",");
							//删除收尾逗号
							if(i == chkList.length - 1) {
								dishIds = dishIds.substr(1);
								dishIds = dishIds.substr(0, dishIds.length - 1);
								dishNames = dishNames.substr(1);
								dishNames = dishNames.substr(0, dishNames.length - 1);
							}
						}
					}
					$("#dishIds").val(dishIds);
					$("#dishNames").val(dishNames);
				});
				
				//验证信息
				$.formValidator.initConfig({
					formID: "queryForm",
					debug:false,
					onError: function(msg, obj, errorlist){
					}
				});
				$("#dishChName").formValidator({empty:true, onShow:"请输入中文菜名", onCorrect:"恭喜你,你输对了"}).inputValidator({max:25, onError:"你输入的中文菜名太长"}).regexValidator({regExp: "Illegal", dataType:"enum", onError:"中文菜名中包含非法字符"});
				$("#dishEnName").formValidator({empty:true, onShow:"请输入英文菜名", onCorrect:"恭喜你,你输对了"}).inputValidator({max:25, onError:"你输入的英文菜名太长"}).regexValidator({regExp: "enname", dataType:"enum", onError:"英文菜名中包含非法字符"});
				$("#priceMin").formValidator({empty:true, tipID:"priceMinTip", onShow: "请输入数字", onCorrect: "恭喜你,你输对了" }).inputValidator({min:0, max:9999, type:"value", onErrorMin:"你输入的值必须大于等于0", onError:"单价必须在0-9999之间，请确认"}).compareValidator({desEmpty:true, desID:"priceMax", dataType:"number", operateor:"<=", onError:"起始单价不能大于结束单价"}).defaultPassed();
				$("#priceMax").formValidator({empty:true, tipID:"priceMaxTip", onShow: "请输入数字", onCorrect: "恭喜你,你输对了" }).inputValidator({min:0, max:9999, type:"value", onErrorMin:"你输入的值必须大于等于0", onError:"单价必须在0-9999之间，请确认"}).compareValidator({desEmpty:true, desID:"priceMin", dataType:"number", operateor:">=", onError:"结束单价不能小于起始单价"}).defaultPassed();
			});
		</script>
	</head>
	
	<body>
		<div class="cms_right">
		  	<div class="cms_title">
		    	<span>菜品管理：</span>
			<input type="text" id="dishIds" />
			<input type="text" id="dishNames" />
		    </div>
		    <div class="cms_search">
    			<form id="queryForm" >
					<table width="1000" border="0" class="search_bd">
						<tr>
				            <td class="yh_name">中文菜名：</td>
				            <td class="yh_bd"><input type="text" id="dishChName" class="cms_text02" maxlength="25"/></td>
				            <td class="tishi"><div id="dishZhNameTip" style="width:200px"></div></td>
				            <td class="yh_name">英文菜名：</td>
				            <td class="yh_bd"><input type="text" id="dishEnName" class="cms_text02" maxlength="25"/></td>
				            <td class="tishi"><div id="dishEnNameTip" style="width:200px"></div></td>
						</tr>
						<tr>
				            <td class="yh_name">起始单价：</td>
				            <td class="yh_bd"><input type="text" id="priceMin" class="cms_text02"/></td>
				            <td class="tishi"><div id="priceMinTip" style="width:200px"></div></td>
				            <td class="yh_name">结束单价：</td>
				            <td class="yh_bd"><input type="text" id="priceMax" class="cms_text02"/></td>
				            <td><div id="priceMaxTip" style="width:200px"></div></td>
						</tr>
						<tr>
							<td class="yh_name">开始时间：</td>
							<td class="yh_bd"><input type="text" id="startDate" class="Wdate" style="cursor: pointer" onFocus="WdatePicker({readOnly:true})"/></td>
							<td class="tishi"><div id="startDateTip" style="width:200px"></div></td>
							<td class="yh_name">结束时间：</td>
							<td class="yh_bd"><input type="text" id="endDate" class="Wdate" style="cursor: pointer" onFocus="WdatePicker({readOnly:true})"/></td>
							<td><div id="endDateTip" style="width:200px"></div></td>
						</tr>
						<tr>
							<td></td>
							<td align="right"><input type="reset" class="search_btn" value="重置" /></td>
							<td align="center" ><input type="button" id="queryBtn" class="search_btn" value="搜索" /></td>
							<td><input type="button" id="saveBtn" class="search_btn" value="确定" /></td>
							<td></td>
							<td></td>
						</tr>
			        </table>
	   			</form>
		    </div>
			<div class="grid-view">
				<table id="pageList" class="items" width="100%" border="1" cellspacing="0" cellpadding="0">
					<tr class="list_tab_title">
						<td bgcolor="#eae9eb"><input type="checkbox" id="chkAll" /></td>
						<td bgcolor="#eae9eb">中文菜名</td>
						<td bgcolor="#eae9eb">英文菜名</td>
						<td bgcolor="#eae9eb">单价</td>
						<td bgcolor="#eae9eb">类型</td>
					</tr>
				</table>
				<div id="page" style="margin: auto;"></div>
			</div>
		</div>
	</body>
</html>
	